package com.sa.web.behind.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sa.web.bean.StuDetailBean;
import com.sa.web.bean.StuInfoBean;
import com.sa.web.dao.service.StuDetailDao;
import com.sa.web.dao.service.StuInfoDao;
import com.sa.web.dao.service.UserDao;
import com.sa.web.util.Page;
import com.sa.web.util.Util;

public class StuManageAction extends ActionSupport {
	/** 序列 */
	private static final long serialVersionUID = 1L;
	/**学生信息dao*/
	private StuInfoDao stuInfoDao;
	/**学生信息资料dao*/
	private StuDetailDao stuDetailDao;
	/**会员信息Dao*/
	private UserDao userDao;
	/** 学生信息bean*/		
	private StuInfoBean stuInfoBean;
	/** 设置学生信息集合 */
	List<StuInfoBean> stuList;
	/** easyUi对象*/
	private JSONArray rows;
	/** 信息总数*/
	private JSONArray total;
	/** 获取返回值*/
	private JSONArray msg;
	/**信息详情LIST*/
	private List<StuDetailBean> stuDetailList;
	/** 学生姓名*/
	private String stuName;
	
	private String userName;
	/** 学生ID*/
	private String userId;
	/** 资料路径url*/
	private String url = "stuFile/";
	
   /**
    * 初始化学生页面
    * @return success
    * @throws Exception
    */
	public String initStuInfoFind() throws Exception {
		return SUCCESS;
	}
	
	 /**
	    * 初始化学生上传信息明细页面(查询页面)
	    * @return success
	    * @throws Exception
	    */
	public String initStuDetailFind() throws Exception {
			return SUCCESS;
	}
	
	 /**
	    * 初始化资料上传页面
	    * @return success
	    * @throws Exception
	    */
	public String initStuDetailUpload() throws Exception {
			return SUCCESS;
	}
	
	/**
	 * 初始化更新学生信息页面
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String initStuInfoUpdate() throws Exception {
		setStuInfoBean((stuInfoDao.getStuInfo(Util.getRequest().getParameter("stuId"))));
		return SUCCESS;
	}
	
	/**
	 * 初始化添加学生信息页面
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String initStuInfoAdd() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 添加学生信息
	 * @return success
	 * @throws Exception
	 */
	public String addStuInfo() throws Exception{
		userDao.modifyUser(stuInfoBean.getUserName());
		stuInfoBean.setStuId(Util.getUUID());
		stuInfoBean.setCreateDate(Util.dateToString(new Date()));
		stuInfoDao.addStuInfo(stuInfoBean);
		return SUCCESS;
	}
	
	/**
	 * 修改学生信息
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String updateStuInfo() throws Exception{
		stuInfoDao.updateStuInfo(stuInfoBean);
		return SUCCESS;
	}
	
	/**
	 * 查找学生信息
	 * @return json
	 * @throws Exception
	 */
	public String findStuInfoList()  throws Exception{
		 stuList = stuInfoDao.getStuInfoList( Util.getRequest().getParameter("stuName"),Page.getStarIndex(),Page.getPreIndex());
		 rows = JSONArray.fromObject(stuList);
		 total = JSONArray.fromObject(stuInfoDao.getStuInfoCount(Util.getRequest().getParameter("userName")));
		 return Util.jSONSuccess();
	}
	
	/** 
	 * 学生上传信息明细
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String viewStuDetailView() throws Exception{
		stuDetailList = stuDetailDao.getStuDetail(Util.getRequest().getParameter("stuName"));
		setStuName(stuDetailList.get(0).getStuName());
		return SUCCESS;
	}
	
	/**
	 * 删除学生信息
	 * @return json
	 * @throws Exception
	 */
	public String deleteStuInfo() throws Exception {
		stuInfoDao.deleteStuInfo(Util.getRequest().getParameter("stuId")==null?"":Util.getRequest().getParameter("stuId").toString());
		msg = JSONArray.fromObject("[\"删除成功\"]");
		return Util.jSONSuccess();
	}
	/** 
	 * 资料上传
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String uploadStuDetail()throws Exception {
		
		ActionContext ac = ActionContext.getContext(); 
		ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT); 
		String path = sc.getRealPath("stuFile");
		String fileType = "";
		File file = new File(path + "/" + userId );
		InputStream fis = null;
		OutputStream fos = null;
		if(stuDetailList != null){
		for(int i=0; i<stuDetailList.size(); i++){
			StuDetailBean stuDetail = stuDetailList.get(i);
			if(stuDetail.getStuFile() != null){
			    stuDetail.setId(Util.getUUID());
			    stuDetail.setCreateDate(Util.dateToString(new Date()));
			    stuDetail.setStuName(stuName);
			    stuDetail.setName(stuDetail.getStuFileFileName());
			    fileType = stuDetail.getName().substring(stuDetail.getName().lastIndexOf("."));
			    stuDetail.setUrl("/" + userId +"/" + Util.getNowString() + i + fileType);
			    stuDetailDao.addStuDetail(stuDetail);
			    if(!file.exists()){
			    	file.mkdirs();
			    }
			    System.out.println(path + stuDetail.getUrl());
			    fos = new FileOutputStream(path + stuDetail.getUrl());
	            fis = new FileInputStream(stuDetail.getStuFile());
	            byte[] buffer = new byte[1024];
	            int len = 0;
	            while ((len = fis.read(buffer)) > 0) {
	                fos.write(buffer, 0, len);
	            }
	            
	            fis.close();
	            fos.close();
			    
			}
		}
		}
		return SUCCESS;
	}
	
/**************************getter and setter****************************************************************************************************/
	public StuInfoDao getStuInfoDao() {
		return stuInfoDao;
	}

	public void setStuInfoDao(StuInfoDao stuInfoDao) {
		this.stuInfoDao = stuInfoDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public JSONArray getRows() {
		return rows;
	}

	public JSONArray getTotal() {
		return total;
	}
	public void setTotal(JSONArray total) {
		this.total = total;
	}
	public JSONArray getMsg() {
		return msg;
	}
	public void setMsg(JSONArray msg) {
		this.msg = msg;
	}
	
	public List<StuDetailBean> getStuDetailList() {
		return stuDetailList;
	}

	public void setStuDetailList(List<StuDetailBean> stuDetailList) {
		this.stuDetailList = stuDetailList;
	}

	public String getStuName() {
		return stuName;
	}

	public void setStuName(String stuName) {
		this.stuName = stuName;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public StuDetailDao getStuDetailDao() {
		return stuDetailDao;
	}

	public void setStuDetailDao(StuDetailDao stuDetailDao) {
		this.stuDetailDao = stuDetailDao;
	}

	public StuInfoBean getStuInfoBean() {
		return stuInfoBean;
	}

	public void setStuInfoBean(StuInfoBean stuInfoBean) {
		this.stuInfoBean = stuInfoBean;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

}
